﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;

namespace Adventure.Server.Library
{
    /// <summary>
    /// List of object ids.
    /// </summary>
    public class ObjectIdList
    {
        #region Constants

        /// <summary>
        /// Character to split Ids.
        /// </summary>
        public const char SplitCharacter = ';';

        #endregion

        #region Fields

        /// <summary>
        /// Logger for this class.
        /// </summary>
        private static readonly TraceSource Logger = new TraceSource(ServerConstants.ServerTraceSourceNameLibrary);

        /// <summary>
        /// Collection of all ObjectIds stored.
        /// </summary>
        private Collection<string> _objectIds= new Collection<string>();

        #endregion

        #region Lifetime

        /// <summary>
        /// Creates a new instance of <see cref="ObjectIdList"/>.
        /// </summary>
        public ObjectIdList()
        {
        }

        /// <summary>
        /// Creates a new instance of <see cref="ObjectIdList"/>.
        /// </summary>
        /// <param name="list">List of Object Ids.</param>
        public ObjectIdList(string list)
        {
            // Check the parameter
            if (string.IsNullOrEmpty(list)) return;

            string[] ids = list.Split(new[] {SplitCharacter}, StringSplitOptions.RemoveEmptyEntries);
            foreach (var id in ids)
                _objectIds.Add(id);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Write all Ids in a single string, using <see cref="SplitCharacter"/> to split ids.
        /// </summary>
        /// <returns>List of all ids.</returns>
        public override string ToString()
        {
            var result = new StringBuilder();
            var empty = true;
            foreach (var id in _objectIds)
            {
                if (empty)
                {
                    result.Append(id);
                    empty = false;
                }
                else
                {
                    result.Append(SplitCharacter);
                    result.Append(id);
                }
            }
            return result.ToString();
        }

        /// <summary>
        /// Add an id to the list of ids.
        /// </summary>
        /// <param name="id">Id to add.</param>
        public void Add(string id)
        {
            _objectIds.Add(id);
        }

        /// <summary>
        /// Removes an id from the list of ids.
        /// </summary>
        /// <param name="id">Id to remove.</param>
        public void Remove(string id)
        {
            _objectIds.Remove(id);
        }

        /// <summary>
        /// Checks if an id is inside the list.
        /// </summary>
        /// <param name="id">Id to check.</param>
        /// <returns></returns>
        public bool Contains(string id)
        {
            return _objectIds.Contains(id);
        }

        #endregion
    }
}
